package com.zhuxiaomei.controller;

import com.zhuxiaomei.context.BaseContext;
import com.zhuxiaomei.dto.user.*;
import com.zhuxiaomei.entity.msg.WageConversionMessage;
import com.zhuxiaomei.result.Result;
import com.zhuxiaomei.result.ReturnCodeEnum;
import com.zhuxiaomei.service.UserService;
import com.zhuxiaomei.vo.user.CoupleInfoVO;
import com.zhuxiaomei.vo.user.MineVO;
import com.zhuxiaomei.vo.user.UserInfoVO;
import com.zhuxiaomei.vo.user.UserLoginVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hxm
 * @date 2024/12/6 19:38
 * @description
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO){
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        if (userLoginVO != null) {
            return Result.success(userLoginVO);
        }
        return Result.fail(ReturnCodeEnum.USERNAME_OR_PASSWORD_ERROR.getCode(), ReturnCodeEnum.USERNAME_OR_PASSWORD_ERROR.getMessage());
    }

    /**
     * 用于检测是否登录的api
     * @return
     */
    @GetMapping("/isLogin")
    public Result<String> isLogin(){
        return Result.success(ReturnCodeEnum.RC200.getCode());
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody UserRegisterDTO userRegisterDTO) {
        ReturnCodeEnum register = userService.register(userRegisterDTO);
        if (register.getCode().equals(ReturnCodeEnum.RC200.getCode())) {
            return Result.success(register.getCode());
        }
        return Result.fail(register.getCode(), register.getMessage());
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/userInfo")
    public Result<UserInfoVO> getUserSetInfo(){
        UserInfoVO userSetVO = userService.getUserInfo(BaseContext.getCurrentId());
        if (userSetVO == null) {
            return Result.fail(ReturnCodeEnum.RC500.getCode(), ReturnCodeEnum.RC500.getMessage());
        }
        return Result.success(userSetVO);
    }

    @PutMapping("/editUserInfo")
    public Result<String> editUserInfo(@RequestBody UserInfoVO userInfoVO){
//        System.out.println(userSetVO);
        Integer row = userService.updateUserInfo(userInfoVO);

        if (row == 0) {
            return Result.fail(ReturnCodeEnum.RC500.getCode(), ReturnCodeEnum.RC500.getMessage());
        }
        return Result.success(ReturnCodeEnum.RC200.getCode());
    }

    @PutMapping("/editUserPasswd")
    public Result<String> editUserPasswd(@RequestBody UserEditPasswdDTO userEditPasswdDTO){
        String res = userService.updateUserPasswd(userEditPasswdDTO);
        return Result.success(res);
    }

    /**
     * mine页面展示
     * @return MineVO，mine页面展示所需要的数据
     */
    @GetMapping("/mineInfo")
    public Result<MineVO> getMineInfo(){
        MineVO mineVO = userService.getMineInfo();
        return Result.success(mineVO);
    }

    /**
     *
     * @return 情侣设置页面的数据VO
     */
    @GetMapping("/coupleInfo")
    public Result<CoupleInfoVO> getCoupleInfo() {
        CoupleInfoVO coupleInfoVO = userService.getCoupleInfo();
        return Result.success(coupleInfoVO);
    }

    /**
     * 根据用户账号返回用户信息
     * @param account
     * @return
     */
    @GetMapping("/userInfo/{account}")
    public Result<UserInfoVO> getUserInfoByAccount(@PathVariable("account") String account){
        UserInfoVO userInfoVO = userService.getUserInfoByAccount(account);
        if (userInfoVO == null) {
            return Result.fail(ReturnCodeEnum.USER_NOT_EXIST.getCode(), ReturnCodeEnum.USER_NOT_EXIST.getMessage());
        }
        return Result.success(userInfoVO);
    }

    /**
     * 是否接受情侣绑定
     * @param coupleBindDTO
     * @return
     */
    @PostMapping("/bindCouple")
    public Result<String> bindCouple(@RequestBody CoupleBindDTO coupleBindDTO){
//        System.out.println(coupleBindDTO);
        Boolean isBind = userService.bindCouple(coupleBindDTO);
        if (isBind) {
            return Result.success(ReturnCodeEnum.RC200.getCode());
        }
        return Result.success(ReturnCodeEnum.REFUSE_BIND_COUPLE.getCode());

    }

    @PutMapping("/editCoupleInfo")
    public Result<String> editCoupleInfo(@RequestBody CoupleInfoVO coupleInfoVO){
        Integer row = userService.updateCoupleInfo(coupleInfoVO);
        if (row == 0) {
            return Result.fail(ReturnCodeEnum.RC500.getCode(), ReturnCodeEnum.RC500.getMessage());
        }
        return Result.success(ReturnCodeEnum.RC200.getCode());
    }

    @GetMapping("/userBalance")
    public Result<UserBalanceDTO> getUserBalance() {
        UserBalanceDTO userBalanceDTO = userService.getUserBalance();
        return Result.success(userBalanceDTO);
    }

    @PostMapping("/wageConversion")
    public Result<String> newWageConversion(@RequestBody WageConversionMessage wageConversionMessage) {
        userService.newWageConversion(wageConversionMessage);
        return Result.success(ReturnCodeEnum.RC200.getCode());
    }

    @GetMapping("/wageConversionMEssageCount")
    public Result<Integer> wageConversionMEssageCount(){
        Integer size = userService.wageConversionMessage().size();
        return Result.success(size);
    }

    @GetMapping("/wageConversionMessage")
    public Result<List<WageConversionMessage>> wageConversionMessage(){
        List<WageConversionMessage> wageConversionMessages = userService.wageConversionMessage();
        return Result.success(wageConversionMessages);
    }

    @PutMapping("/editWageConversionMessage")
    public Result<String> editWageConversionMessage(@RequestBody WageConversionMessage wageConversionMessage){
        userService.editWageConversionMessage(wageConversionMessage);
        return Result.success(ReturnCodeEnum.RC200.getCode());
    }
}
